package com.mooc.lib_network;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;

/**
 * @ClassName: UrlCreator$
 * @Description: java类作用描述
 * @Author: 魏靳元
 * @CreateDate: 2022/8/15 16:59
 * @UpdateRemark: 更新内容
 * @Version: 1.0
 */
public class Urlsplicing {
    public static String createUrlFromParams(String url, Map<String,Object> params){
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(url);
        if(url.indexOf("?")>0||url.indexOf("&")>0){
            stringBuilder.append("&");
        }else {
            stringBuilder.append("?");
        }
        for (Map.Entry<String, Object> entry : params.entrySet()) {
            try {
                String value = URLEncoder.encode(String.valueOf(entry.getValue()), "UTF-8");
                stringBuilder.append(entry.getKey()).append("=").append(value)
                .append("&");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

        }
        stringBuilder.deleteCharAt(stringBuilder.length()-1);
        return stringBuilder.toString();
    }

} 
